package com.hand.springcloud.controller;


import com.hand.springcloud.entities.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @ Description   :  控制层-消费者
 * @ Author        :  xiong.yilin@hand-china.com
 * @ CreateDate    :  2019/9/6 17:13
 */
@RestController
public class ProductController_Consumer {

    // private static final String REST_URL_PREFIX="http://localhost:8001";

    // 修改调用 REST 风格的服务提供者地址 ???这里不区分大小写吗
    // Ribbon 和 Eureka 整合后 ，消费者 Consumer 可以直接调用提供者服务，而不用再关心地址和端口号
    private static final String REST_URL_PREFIX="http://microservice-product";

    @Autowired
    private RestTemplate restTemplate;

    // (url, requestMap, ResponseBean.class)这三个参数分别代表：
    // REST请求地址、请求参数、HTTP响应转换被转换成的对象类型。
    @RequestMapping(value = "/consumer/product/add")
    public boolean add(Product product){
        return restTemplate.postForObject(REST_URL_PREFIX+"/product/add",product,Boolean.class);
    }

    @RequestMapping(value = "/consumer/product/get/{id}")
    public Product get(@PathVariable("id") Long id){
        return restTemplate.getForObject(REST_URL_PREFIX+"/product/get"+id,Product.class);
    }

    @RequestMapping(value = "/consumer/product/list")
    public List<Product> list(){
        return restTemplate.getForObject(REST_URL_PREFIX+"product/list",List.class);
    }
}
